﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using SqlCon;

namespace yfxt
{
    public partial class ms_dzcfcx : Form
    {
        public ms_dzcfcx()
        {
            InitializeComponent();
        }
        SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);

        int i;//选中发票号码或是处方号码的标志
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show("您还没有输入处方号码或是发票号码 ，请输入再查询！");
                return;
            }

            if (radioButton1.Checked != true && radioButton2.Checked != true)
            {
                MessageBox.Show("您还没选查询方式，请选择查询方式，点击处方号码或是发票号码");

                return;
            }

            if (radioButton1.Checked)
            {

                i = 0;//选中发票号码的标志



                get_data(i);


            }

            else if (radioButton2.Checked)
            {
                i = 1;//选中处方号码的标志
                get_data(i);

            }

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            get_cfsb();

            //MessageBox.Show(dataGridView1.CurrentRow.Cells["fphm"].Value.ToString());
        }



        //根据发票号码号码/处方号码取得处方数据

        private void get_data(int i)
        {

            if (conn.State != ConnectionState.Open) { conn.Open(); }

            SqlDataAdapter da = new SqlDataAdapter();

            if (i == 0)
            {
                string s_fphm = "select mzhm,fphm,cfhm,ms_brda.brxm,kfrq,fyrq,ygxm  from ms_brda ,ms_cf01,gy_ygdm " +
                                 "where ms_brda.brid=ms_cf01.brid and  ms_cf01.ysdm=gy_ygdm.ygdm and djybz='1'and fphm=@fphm" +
                                 " and kfrq between @kfrq_01 and @kfrq_02";

                using (SqlCommand cmd = new SqlCommand(s_fphm, conn))
                {
                    cmd.Parameters.AddWithValue("@fphm", textBox1.Text.ToString());
                    cmd.Parameters.AddWithValue("@kfrq_01", DateTime.Parse(dateTimePicker1.Text));
                    cmd.Parameters.AddWithValue("@kfrq_02", DateTime.Parse(dateTimePicker2.Text));

                    da.SelectCommand = cmd;


                }


            }
            else if (i == 1)
            {
                string s_cfhm = "select mzhm,fphm,cfhm,ms_brda.brxm,kfrq,fyrq,ygxm  from ms_brda ,ms_cf01,gy_ygdm " +
                                "where ms_brda.brid=ms_cf01.brid and  ms_cf01.ysdm=gy_ygdm.ygdm and djybz='1'and cfhm=@cfhm" +
                                " and kfrq between @kfrq_01 and @kfrq_02";

                using (SqlCommand cmd = new SqlCommand(s_cfhm, conn))
                {

                    cmd.Parameters.AddWithValue("@cfhm", textBox1.Text.ToString());
                    cmd.Parameters.AddWithValue("@kfrq_01", DateTime.Parse(dateTimePicker1.Text));
                    cmd.Parameters.AddWithValue("@kfrq_02", DateTime.Parse(dateTimePicker2.Text));

                    da.SelectCommand = cmd;

                }
            }


            DataTable dt = new DataTable();

            da.Fill(dt);

            dataGridView1.DataSource = dt;

            dataGridView1.Columns[0].HeaderCell.Value = "门诊号码";

            dataGridView1.Columns[1].HeaderCell.Value = "发票号码";

            dataGridView1.Columns[2].HeaderCell.Value = "处方号码";

            dataGridView1.Columns[3].HeaderCell.Value = "病人姓名";

            dataGridView1.Columns[4].HeaderCell.Value = "开方日期";

            dataGridView1.Columns[5].HeaderCell.Value = "发药日期";

            dataGridView1.Columns[6].HeaderCell.Value = "医生姓名";

        }



        //取得dataGridView1.CurrentRow.Cells["fphm"].Value.ToString()里面的发票号码 然后找到相对应的处方识别序号的处方明细信息

        private void get_cfsb()
        {
            if (conn.State != ConnectionState.Open) { conn.Open(); }

            string s = "select @cfsb=cfsb from ms_cf01 where fphm=@fphm";

            using (SqlCommand cmd = new SqlCommand(s, conn))
            {
                if (dataGridView1.Rows.Count == 0) { return; }
                cmd.Parameters.AddWithValue("@fphm", dataGridView1.CurrentRow.Cells["fphm"].Value.ToString());//选中当前行 触发事件

                SqlParameter sp = new SqlParameter();

                sp = cmd.Parameters.Add("@cfsb", SqlDbType.NVarChar, 40);

                sp.Direction = ParameterDirection.Output;

                cmd.ExecuteNonQuery();

                string s_fphm = "select ypmc,ypgg,ypdw,ypsl,ypdj,sum(ypsl*ypdj)as ypje,zfbl " +
                                   "from ms_cf02,yk_typk where ms_cf02.ypxh=yk_typk.ypxh and cfsb=@cfsb " +
                                   "group by ypmc,ypgg,ypdw,ypsl,ypdj,zfbl";


                using (SqlCommand cmd_01 = new SqlCommand(s_fphm, conn))
                {
                    cmd_01.Parameters.AddWithValue("@cfsb", sp.Value.ToString());

                    SqlDataAdapter da = new SqlDataAdapter();

                    da.SelectCommand = cmd_01;

                    DataTable dt = new DataTable();

                    da.Fill(dt);

                    dataGridView2.DataSource = dt;


                    dataGridView2.Columns[0].HeaderCell.Value = " 药品名称";

                    dataGridView2.Columns[1].HeaderCell.Value = "药品规格";

                    dataGridView2.Columns[2].HeaderCell.Value = " 药品单位";

                    dataGridView2.Columns[3].HeaderCell.Value = "药品数量";

                    dataGridView2.Columns[4].HeaderCell.Value = "药品单价";

                    dataGridView2.Columns[5].HeaderCell.Value = " 药品金额";

                    dataGridView2.Columns[6].HeaderCell.Value = " 自负比例";


                    if (dataGridView1.Rows.Count == 0)
                    {
                        return;
                    }
                    else
                    {
                        this.timer2.Interval = 100;

                        this.timer2.Enabled = true;
                    }
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (conn.State != ConnectionState.Open) { conn.Open(); }

            SqlDataAdapter da = new SqlDataAdapter();

            string s_fphm = "select mzhm,fphm,cfhm,ms_brda.brxm,kfrq,fyrq,ygxm  from ms_brda ,ms_cf01,gy_ygdm " +
                             "where ms_brda.brid=ms_cf01.brid and  ms_cf01.ysdm=gy_ygdm.ygdm and djybz='1'" +
                             " and kfrq between @kfrq_01 and @kfrq_02";

            using (SqlCommand cmd = new SqlCommand(s_fphm, conn))
            {
                cmd.Parameters.AddWithValue("@kfrq_01", DateTime.Parse(dateTimePicker1.Text));
                cmd.Parameters.AddWithValue("@kfrq_02", DateTime.Parse(dateTimePicker2.Text));

                da.SelectCommand = cmd;


                DataTable dt = new DataTable();

                da.Fill(dt);

                dataGridView1.DataSource = dt;

                dataGridView1.Columns[0].HeaderCell.Value = "门诊号码";

                dataGridView1.Columns[1].HeaderCell.Value = "发票号码";

                dataGridView1.Columns[2].HeaderCell.Value = "处方号码";

                dataGridView1.Columns[3].HeaderCell.Value = "病人姓名";

                dataGridView1.Columns[4].HeaderCell.Value = "开方日期";

                dataGridView1.Columns[5].HeaderCell.Value = "发药日期";

                dataGridView1.Columns[6].HeaderCell.Value = "医生姓名";

            }


            get_cfsb();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (DataGridViewRow drw in dataGridView1.Rows)//显示每一行数
            {
                for (int t = 0; t < dataGridView1.Rows.Count; t++)
                    dataGridView1.Rows[t].HeaderCell.Value = Convert.ToString(t + 1);
            }

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            foreach (DataGridViewRow drw in dataGridView2.Rows)//显示每一行数
            {
                for (int t = 0; t < dataGridView2.Rows.Count; t++)
                    dataGridView2.Rows[t].HeaderCell.Value = Convert.ToString(t + 1);
            }
        }

        private void ms_dzcfcx_Load(object sender, EventArgs e)
        {
            this.timer1.Interval = 100;
            this.timer1.Enabled = true;

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}
